/**
 * 首页的控制部分
 */
  var user_id='11204050220';
     $(function(){
    	 paint();
    	 $('#footer_toggle').click(function(){
    		 $(this).attr('class')=="footer_up" ? 
    				  ($(this).attr('class','footer_down'),
    			       $('footer div').css('height','100%'),
    			       $('footer div.right_div').find('label:eq(0)').css('display','none'),
    			       $('footer div.right_div').find('label:eq(1)').css('display','block'))
    				 :($(this).attr('class','footer_up'),$('footer div').css('height','4.5em'),
    				    $('footer div.right_div').find('label:eq(0)').css('display','block'),
    	    			$('footer div.right_div').find('label:eq(1)').css('display','none'));
    		 
    	 });
    	  
    	
    	   $.ajax({
    			 url : '/College/home/getXNXQ',
    			 data:{userid:user_id},
    			 type : 'post'
    		  }).done(
    				function(data) {
    					paint_bar({userid:user_id,xn:data.now[0].xn,xq:data.now[0].xq});
    					for(var m=0;m<data.list.length;m++){
    						if(data.now[0].xn==data.list[m].xn&&data.now[0].xq==data.list[m].xq){
    							
    							$('select#study_select').append('<option value="'+data.list[m].xn+'" xq="'+data.list[m].xq+'" selected="selected">'+data.list[m].info+'</option>');   
    						}
    						else
    					        $('select#study_select').append('<option value="'+data.list[m].xn+'" xq="'+data.list[m].xq+'">'+data.list[m].info+'</option>');
    					 
    				   }
    					$('select#study_select').change(function(){
    						$('#my_study_plan').empty();
    						paint_bar({userid:user_id,xn:$(this).find('option:selected').attr('value'),xq:$(this).find('option:selected').attr('xq')});
    					  });
    				});
    	   $('.rbcontainer a.search_info').click(function(){
     	      paint_select(user_id);
     		   return false;
     	   });
     	   $('#myClass button').click(function(){
     		   $('#myClass').hide('slow');
     	   });
     	   $('#myClass img').click(function(){
     		   $('#myClass').hide('slow');
     	   });
     });
     function paint_bar(param){
			
			$.ajax({
				url : '/College/home/getXXJH',
				data:param,
				type : 'post'
			}).done(function(data) {
						var c = echarts.init($('#my_study_plan')[0], 'macarons');
						c.hideLoading();
						var option = Config('学习目标（'+param.xn+'学年）',false,'bar').option6(data);
						c.setOption(option, true);
						$(window).resize(function() {
							c.resize();
							c.setOption(option);
						});
					});
		}
	 function paint(){
		 $.ajax({
			 url:'/College/home/getCourseOfNowDay',
			 data:{userid:user_id},
			 type:'post'
		 }).done(function(data){
			 if(data.length==0){
				  $('.my_table tbody').append('<tr></tr>');
				 $('.my_table tbody tr:eq(0)').append('<td>自由活动</td>');
			     $('.my_table tbody tr:eq(0)').append('<td>随意</td>');
			     $('.my_table tbody tr:eq(0)').append('<td>一整天</td>');
				 }
			 else{
			  for(var i1=0;i1<data.length;i1++){
			     $('.my_table tbody').append('<tr></tr>');
			     $('.my_table tbody tr:eq('+i1+')').append('<td>'+data[i1].class_name+'</td>');
			     $('.my_table tbody tr:eq('+i1+')').append('<td>'+data[i1].class_address+'</td>');
			     $('.my_table tbody tr:eq('+i1+')').append('<td>'+data[i1].class_time+'</td>');
			  }}
		 });
		 $.ajax({
			 url:'/College/home/getDate',
			 data:{userid:user_id},
			 type:'post'
		 }).done(function(data){
		
			 $('.rtcontainer div:eq(0)').append('<span class="data_now">'+data.year_month+'</span>');
 		     $('.rtcontainer div:eq(0)').append('<span class="now_data">'+data.day+'</span>');
 		     $('.rtcontainer div.right_date').append('<span class="the_data">'+data.xn+'  '+ data.djz+'</span>');
 		     $('.rtcontainer div.right_date').append('<span>'+data.nl+'</span><span>忌 : '+data.j+'</span>');
 		     $('.rtcontainer div.right_date').append('<span class="avoid">宜 : '+data.y+'</span>');
			
		 });
		 $.ajax({
			 url:'/College/message/getNewMessage',
			 data:{userid:user_id},
			 type:'post'
		 }).done(function(data){
			
			 if(data.content.length!='0'){
			   $('.rccontainer .search_info').before(' <a href="#"> ( '+data.content.length+' )</a>');
			   var message_length=data.content.length-1;
			   for(var i2=0;i2<=message_length;i2++)
  			      $('.rccontainer div.message_scroll').append('<span>'+data.content[i2].n_content+'</span>');
			    // setInterval(function(){ the_scroll(message_length);} ,3000);
			   }
			 else
			   $('.rccontainer').hide(); 
		 });
		 paint_functions({userid:user_id},'/College/home/getUserFunction');
		
	 };
	 
/*	 function paint_functions(data,url){
		 $('div.footer_left').empty();$('div.my_function').empty();$('div.other_function').empty();
		 $('div.footer_left').append('<a href="" id="function_add"><img  src="img/add.png" /></a>');
		 $('div.my_function').append('<p style="margin-top:-1.5em">我的应用</p>');
		 $('div.other_function').append('<p>其他应用</p>');
		  $.ajax({
			 url:url,
			 data:data,
			 traditional: true,
			 type:'post'
		  }).done(function(data){
			  for(var i3=0;i3<data.owned.length;i3++){
					 $('.footer_left a#function_add').before('<a href="'+data.owned[i3].function_path+
							 '"><img  src="'+data.owned[i3].function_face+'" /><label>'+data.owned[i3].function_name+'</label></a>');
					   $('div#myFunction div.my_function p').after('<div class="the_functions" data-value="'+data.owned[i3].function_id+'">'+
						'<img  src="'+data.owned[i3].function_face+'" /><label></label><a href="'+data.owned[i3].function_path+'">'+data.owned[i3].function_name+'</a></div>');
				 }
	             for(var i4=0;i4<data.remainder.length;i4++){
	            	 $('div#myFunction div.other_function p').after('<div class="the_functions" data-value="'+data.remainder[i4].function_id+'">'+
	 						'<img  src="'+data.remainder[i4].function_face+'" /><label></label><a href="'+data.remainder[i4].function_path+'">'+data.remainder[i4].function_name+'</a></div>');
				 }
	             $('#function_add').unbind('click').click(function(){
		    		 $('#myFunction').show('slow');
		    	     controls('my_function','other_function');
		    	     controls('other_function','my_function');
		    	     $('div#myFunction h3 i.fa-times').click(function(e){
		    	    	 $('#myFunction').hide('fast');
		    	    	 e.stopPropagation();
		    	     });
		    		 $('button.function_finish').unbind('click').click(function(){
		    			 var $my_functions=$('#myFunction div.my_function ');
		    			 var my_function_length=$my_functions.find('div.the_functions').length;
		    			   var detial = new Array();
		    	            for(var i6=0;i6<my_function_length;i6++){
		    		           var detail_row = new Object();
		    		           detail_row.function_id = $my_functions.find('div.the_functions:eq('+i6+')').attr('data-value');
		    		           detail_row.function_name = $my_functions.find('div.the_functions:eq('+i6+')').find('a').text();
		    		           detail_row.function_face = $my_functions.find('div.the_functions:eq('+i6+')').find('img').attr('src');
		    		           detail_row.function_path = $my_functions.find('div.the_functions:eq('+i6+')').find('a').attr('href');
		    		           detial.push(detail_row);
		    		         }
		    	         
		    			 $('#myFunction').hide('fast');
		    			 paint_functions({userid:user_id,function_list:JSON.stringify(detial)},'/College/home/setUserFunction');
		    			
		    		 });
		    		 $('.function_search').click(function(){
		    			 alert($(this).prev().val()); 
		    		 });
		    		 return false;
		    	 });
		  });
	 };*/
	 function paint_functions(data,url){
		 $('div.footer_left').empty();$('div.my_function').empty();$('div.other_function').empty();
		 $('div.footer_left').append('<a href="" id="function_add"><img  src="img/add.png" /></a>');
		 $('div.my_function').append('<p style="margin-top:-1.5em">我的应用</p>');
		 $('div.other_function').append('<p>其他应用</p>');
		  $.ajax({
			 url:url,
			 data:data,
			 traditional: true,
			 type:'post'
		  }).done(function(data){
			  for(var i9=0;i9<data.owned.length;i9++)
			     $('.footer_left a#function_add').before('<a href="'+data.owned[i9].function_path+
							 '" data-id="'+data.owned[i9].function_id+'"><img  src="'+data.owned[i9].function_face+'" /><label>'+data.owned[i9].function_name+'</label></a>');
	             $('#function_add').unbind('click').click(function(){
	            	 paint_function_dialog({userid:user_id},'/College/home/getUserFunction');
		    		 return false;
		    	 });
		  });
	 };
	 function paint_function_dialog(data,url){
		 $('div#myFunction div.the_functions ').remove();
		 $.ajax({
			 url:url,
			 data:data,
			 traditional: true,
			 type:'post'
		  }).done(function(data){
			  for(var i3=0;i3<data.owned.length;i3++){
					$('div#myFunction div.my_function p').after('<div class="the_functions" data-value="'+data.owned[i3].function_id+'">'+
					'<img  src="'+data.owned[i3].function_face+'" /><label></label><a href="'+data.owned[i3].function_path+'">'+data.owned[i3].function_name+'</a></div>');
			  }
	          for(var i4=0;i4<data.remainder.length;i4++){
	            	$('div#myFunction div.other_function p').after('<div class="the_functions" data-value="'+data.remainder[i4].function_id+'">'+
	 				'<img  src="'+data.remainder[i4].function_face+'" /><label></label><a href="'+data.remainder[i4].function_path+'">'+data.remainder[i4].function_name+'</a></div>');
			 }
		 $('#myFunction').show('slow');
	     controls('my_function','other_function');
	     controls('other_function','my_function');
	   
		 $('button.function_finish,div#myFunction h3 i.fa-times').unbind('click').click(function(e){
			 var $my_functions=$('#myFunction div.my_function ');
			 var my_function_length=$my_functions.find('div.the_functions').length;
			   var detial = new Array();
	            for(var i6=0;i6<my_function_length;i6++){
		           var detail_row = new Object();
		           detail_row.function_id = $my_functions.find('div.the_functions:eq('+i6+')').attr('data-value');
		           detail_row.function_name = $my_functions.find('div.the_functions:eq('+i6+')').find('a').text();
		           detail_row.function_face = $my_functions.find('div.the_functions:eq('+i6+')').find('img').attr('src');
		           detail_row.function_path = $my_functions.find('div.the_functions:eq('+i6+')').find('a').attr('href');
		           detial.push(detail_row);
		         }
			 $('#myFunction').hide('fast');
			 $.ajax({
			    url:'/College/home/setUserFunction',
			    data:{userid:user_id,function_list:JSON.stringify(detial)}
			 });
			 e.stopPropagation();
		    });
		     dialog_move($('div#myFunction div.my_dialog'));
		 });
	 }
	 function controls(class_name1,class_name2){
		 $('#myFunction .'+class_name1).find('label').unbind('click');
		 $('#myFunction .'+class_name1).find('label').click(function(event){
			 $(this).unbind('click').parent('div').appendTo('#myFunction .'+class_name2);
			 if(class_name1=='my_function')
			     $('.footer_left ').find('a[data-id='+$(this).unbind('click').parent('div').attr('data-value')+']').remove();
			 else
				 $('.footer_left a#function_add').before('<a href="'+$(this).unbind('click').parent('div').find('a').attr('href')+
						 '"  data-id="'+$(this).unbind('click').parent('div').attr('data-value')+
						 '"><img  src="'+$(this).unbind('click').parent('div').find('img').attr('src')+'" /><label>'+
						 $(this).unbind('click').parent('div').find('a').text()+'</label></a>');
			 controls(class_name2,class_name1);
			     
	      });
	 };
	 function the_scroll(length){
		 var the_length=length;
		 var $the_span=$('div.message_scroll').find('span:eq(0)');
		 while(the_length){
			 $the_span.delay(3000).animate({'margin-left':'-=100'+'%'},2000);
			 the_length--;
		 } 
			 $the_span.delay(3000).animate({'margin-left':'0'},2000);
	   };
	   function paint_class(data){
		   $('#myClass table tbody').empty();
	    	  $.ajax({
	    		 url:'/College/home/getCourse',
	    		 data:data,
	    		 type:'post'
	    	  }).done(function(data){
	    		
	    		  for(var i7=0;i7<data.length;i7++){
	    			  $('#myClass table tbody').append('<tr></tr>');
	    			  var $the_father=$('#myClass table tbody tr:eq('+i7+')');
	    			  $the_father.append('<th>'+data[i7].the_time+'</th>');
	    			  data[i7].class_content.xq1.address==''? $the_father.append('<th></th>'):
	    					  $the_father.append('<th>'+data[i7].class_content.xq1.course+'<br/>( '+data[i7].class_content.xq1.address+' )</th>');
	    			  data[i7].class_content.xq2.address==''? $the_father.append('<th></th>'):
	    				      $the_father.append('<th>'+data[i7].class_content.xq2.course+'<br/>( '+data[i7].class_content.xq2.address+' )</th>');
	    			  data[i7].class_content.xq3.address==''? $the_father.append('<th></th>'):
	    			           $the_father.append('<th>'+data[i7].class_content.xq3.course+'<br/>( '+data[i7].class_content.xq3.address+' )</th>');
	    			  data[i7].class_content.xq4.address==''? $the_father.append('<th></th>'):
	    			           $the_father.append('<th>'+data[i7].class_content.xq4.course+'<br/>( '+data[i7].class_content.xq4.address+' )</th>');
	    			  data[i7].class_content.xq5.address==''? $the_father.append('<th></th>'):
	    			           $the_father.append('<th>'+data[i7].class_content.xq5.course+'<br/>( '+data[i7].class_content.xq5.address+' )</th>');
	    			  data[i7].class_content.xq6.address==''? $the_father.append('<th></th>'):
	    			           $the_father.append('<th>'+data[i7].class_content.xq6.course+'<br/>( '+data[i7].class_content.xq6.address+' )</th>');
	    			  data[i7].class_content.xq7.address==''? $the_father.append('<th></th>'):
	    			           $the_father.append('<th>'+data[i7].class_content.xq7.course+'<br/>( '+data[i7].class_content.xq7.address+' )</th>');
	    	     }
	    		  $('#myClass').show('slow');
	    	  });
	      };
	      function paint_select(week){
	    	  
	    	  $.ajax({
	    		  url:'/College/home/getWeeks',
	    		  data:{userid:week},
	    		  type:'post'
	    	  }).done(function(data){
	    		  paint_class({userid:week,weeknum:data[0].week_num});
	    		  for(var i5=0;i5<data.length;i5++){
	    			  if(data[i5].bjzd=='1'){
	    				  $('#myClass .my_dialog h3').text(data[i5].xn);
	    			    $('#myClass select').append('<option value="'+data[i5].week_num+'" selected="selected" >'+data[i5].week_string+'</option>');
	    			    }
	    			  else
	    				$('#myClass select').append('<option value="'+data[i5].week_num+'">'+data[i5].week_string+'</option>'); 
	    		  }
	    		 $('#myClass div.my_dialog select').change(function(){
	    	    	  paint_class({userid:week,weeknum:$(this).find('option:selected').attr('value')})
	    	      })
	    	  });
	      };
	      function dialog_move(object){
	    		object.hover(function(){
	    			$(this).css('cursor','move');
	    		},function(){
	    			$(this).css('cursor','default');
	    		});
	    		object.mousedown(
	    		            function(event){
	    		    	//	$(this).css('cursor','move');
	    		                var isMove = true;
	    		                var screenW =$(document).width(); 
	    		                var screenH = $(document).height();
	    		                var dialogW=object.outerWidth(true);
	    		                var dialogH=object.outerHeight(true);
	    		                var abs_x = event.pageX - object.offset().left; 
	    		                var abs_y = event.pageY - object.offset().top;
	    		    			
	    		                $(document).mousemove(function (event) {  
	    		                        if (isMove) {  
	    		                            var obj = object; 
	    		                            var left_x = event.pageX - abs_x;
	    		                            var top_y = event.pageY - abs_y;
	    		    					//	console.log(top_y);
	    		                            if((screenW-dialogW)<=left_x){
	    		                                left_x = screenW-dialogW;
	    		                            }
	    		                            if((screenH-dialogH)<=top_y){
	    		                                top_y = screenH-dialogH;
	    		                            }
	    		                            if(left_x<0){
	    		                                left_x=20;
	    		                            }
	    		                            if(top_y<0){
	    		                                top_y=10;
	    		                            }
	    		    						
	    		                            obj.css({'left':left_x, 'top':top_y});  
	    		                        }  
	    		                    }
	    		                 ).mouseup(  
	    		                                function () {  
	    		                                	
	    		                                       isMove = false;  
	    		                                    }  
	    		                                );
	    		            }
	    		        );
	    	};